package com.cloudspace.jindun.activity.interphone;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.error.VolleyError;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.CustomTitleBaseActivity;
import com.cloudspace.jindun.model.CommonResult;
import com.cloudspace.jindun.network.api.API;
import com.cloudspace.jindun.network.base.ApiCallback;
import com.cloudspace.jindun.utils.MMAlert;
import com.cloudspace.jindun.utils.ToastMaker;

/**
 * Created by zengxianhua on 17/2/11.
 */

public class InterphoneRemarkActivity extends CustomTitleBaseActivity implements View.OnClickListener {
    public static final String TAG = InterphoneRemarkActivity.class.getSimpleName();

    private Button mConfirmBtn;
    private EditText mChannelMarkEt;
    private String mChannelId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.duijiang_channel_details_change_remark, true);

        getTitleBar().setSimpleMode(getResources().getString(R.string.channel_change_remark));

        mConfirmBtn = (Button) findViewById(R.id.duijiang_btn_channel_details_change_remark);
        mConfirmBtn.setOnClickListener(this);

        mChannelMarkEt = (EditText) findViewById(R.id.duijiang_et_channel_remark);

        if (getIntent() != null){
            mChannelId = getIntent().getStringExtra("channelid");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.duijiang_btn_channel_details_change_remark:
                changeChannelMark();
                break;

            default:
                break;
        }
    }

    private void changeChannelMark() {
        String channelMark = mChannelMarkEt.getEditableText().toString();
        if (TextUtils.isEmpty(channelMark)){
            ToastMaker.showToastShort(R.string.channel_remark_empty);
        }


        API.getInstance().getChannelApi().modifyChannelRemark(TAG, mChannelId, channelMark, new ApiCallback<CommonResult>() {
            @Override
            public void handleResult(CommonResult result, VolleyError error) {
                if (result != null && result.status == 200){
                    showChangeDialog(R.string.change_success, true);
                }else if (error != null){
                    showChangeDialog(R.string.change_fail, false);
                }
            }
        });
    }

    public static void showActivity(Context context, String channelId){
        Intent intent = new Intent(context, InterphoneRemarkActivity.class);

        if (!(context instanceof Activity)){
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

        intent.putExtra("channelid", channelId);
        context.startActivity(intent);
    }

    private void showChangeDialog(int stringId,final boolean success) {
        MMAlert.showAlert(
                this,
                getResources().getString(stringId),
                getResources().getString(R.string.info),
                getResources().getString(R.string.iknow),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (success){
                            finish();
                        }
                    }
                }
        );
    }
}
